package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase
{
	public static ApplicationManager app;
	public Random rnd = new Random();
	
	@BeforeTest
	public void setUp() throws Exception
	{
		app = new ApplicationManager();
	}

	@AfterTest
	public void tearDown() throws Exception
	{
		app.stop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator()
	{
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++)
		{
			GroupData group = new GroupData()
				.withName(generateRandomString())
				.withHeader(generateRandomString())
				.withFooter(generateRandomString());
			list.add(new Object[]{group});
		}
		return list.iterator();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator()
	{
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++)
		{
			ContactData contact = new ContactData();
			contact.firstName = generateRandomString();
			contact.lastName = generateRandomString();
			contact.address = generateRandomString();
			contact.homePhone = generateRandomInt();
			contact.mobilePhone = generateRandomInt();
			contact.workPhone = generateRandomInt();
			contact.email1 = generateRandomString();
			contact.email2 = generateRandomString();
			contact.birthDay = generateRandomDay();
			contact.birthMonth = generateRandonMonth();
			contact.birthYear = generateRandomInt();
			contact.group = generateRandomGroup();
			
			contact.address2 = generateRandomString();;
			contact.phone2 = generateRandomInt();
			
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}

	private String generateRandomDay()
	{
		String day = String.valueOf(rnd.nextInt(31));
		if (day == "0")
		{
			day = "-";
		}
		return day;
	}

	public String generateRandonMonth()
	{
		int month = rnd.nextInt(13);
		String monthString;
		switch (month) 
		{
			case 0:  monthString = "-";
					 break;    
			case 1:  monthString = "January";
		             break;
		    case 2:  monthString = "February";
		             break;
		    case 3:  monthString = "March";
		             break;
		    case 4:  monthString = "April";
		             break;
		    case 5:  monthString = "May";
		             break;
		    case 6:  monthString = "June";
		             break;
		    case 7:  monthString = "July";
		             break;
		    case 8:  monthString = "August";
		             break;
		    case 9:  monthString = "September";
		             break;
		    case 10: monthString = "October";
		             break;
		    case 11: monthString = "November";
		             break;
		    case 12: monthString = "December";
		             break;
		    default: monthString = null;
		             break;
		}
		return monthString;
	}
	
	public String generateRandomGroup()
	{
		List<WebElement> elements = app.driver.findElements(By.xpath("//select[@name = 'new_group']"));
		for (WebElement element : elements)
		{
			List<WebElement> options = element.findElements(By.tagName("option"));
			int index = rnd.nextInt(options.size());
			if (index == 0)
			{
				return null;
			}
			String value = options.get(index).getText();
			return value;
		}
		return null;
	}
	
	public String generateRandomInt()
	{
		if (rnd.nextInt(5) == 0)
		{
			return null;
		}
		else
		{
			return String.valueOf(rnd.nextInt());
		}
	}
	
	public String generateRandomString()
	{
		if (rnd.nextInt(3) == 0)
		{
			return "";
		}
		else
		{
			return "test" + rnd.nextInt();
		}
	}
}
